#version 450 compatibility





#define BlockNum 16

layout(local_size_x = DEF_LocalSize, local_size_y = DEF_LocalSize, local_size_z = 1) in;

layout( binding = 0, rgba8ui) uniform uimage2D Pix;

layout( binding = 0 ) uniform Buf_LoalParameter0 {
    ivec4 m_Box;
    ivec2 m_Offset;
    uint m_PointNum;
    uint m_IntersectPointNum;
    uint m_Func;
};

layout( binding = 1 ) uniform Buf_LoalParameter1 {
    uint m_Color;
};

layout(std430, binding = 2) buffer Buf_GlobalParameter2 {
    uvec2 m_Hash[];
};



shared vec2 tmpPosA;
shared vec2 tmpPosB;
shared ivec2 posA;
shared ivec2 posB;

shared int FillWidth;// = abs(posB.x - posA.x);
shared uint global_localID;// = 0;

void main(){
    const uint id = (gl_WorkGroupID.y * gl_NumWorkGroups.x) + gl_WorkGroupID.x;

    uint localID = gl_LocalInvocationIndex;

    if(localID == 0){
        tmpPosA = unpackHalf2x16(m_Hash[id*2].x);
        tmpPosB = unpackHalf2x16(m_Hash[id*2+1].x);
        posA = ivec2(round(tmpPosA.x), round(tmpPosA.y));
        posB = ivec2(round(tmpPosB.x), round(tmpPosB.y));

        FillWidth = abs(posB.x - posA.x);
        global_localID = 0;
    }
    barrier();
    
    for(uint i=0; ; ){
        i = global_localID + localID;
        if(i < FillWidth){
            if (i < abs(posA.x - posB.x)) {
                i *= sign(posB.x - posA.x);
                
                ivec2 pos = ivec2(i, posA.y);
                
                pos.x += int(tmpPosA.x);
                pos += m_Offset;
                if(pos.x >= m_Box.x && pos.y >= m_Box.y && pos.x < m_Box.z && pos.y < m_Box.w){
                    
                    uvec4 c = uvec4(bitfieldExtract(m_Color, 0, 8), 
                            bitfieldExtract(m_Color, 8, 8), 
                            bitfieldExtract(m_Color, 16, 8), 
                            bitfieldExtract(m_Color, 24, 8));

                    imageStore(Pix, pos, c);

                }
            }
        }
        barrier();

        if(global_localID >= FillWidth) break;
        if(localID == 0){
            global_localID += DEF_LocalSize*DEF_LocalSize;
        }
        barrier();
    }

}


